/* Author:

*/

// JavaScript Document

$(document).ready( function() {

	//remoteRequest('http://trevorburkholder.blogspot.com/feeds/posts/default?max-results=5', parseBLOG);
	blogRequest(parseBLOG);
	localRequest('xml/git.xml', parseGIT);
	localRequest('xml/about.xml', parseABOUT);

	// set AJAX properties and query string variables for curl.php
	/*function remoteRequest(url, parser) {

	 $.ajax({
	 type: "GET",
	 url: "php/remoteRequest.php",
	 dataType: "html",
	 data: {
	 url: url
	 },
	 success: parser
	 }); // close ajax
	 }*/

	function blogRequest(parser) {

		$.ajax({
			type: "GET",
			url: "php/blogRequest.php",
			dataType: "html",
			success: parser
		}); // close ajax
	}

	function localRequest(url, parser) {
		$.ajax({
			type: "GET",
			url: url,
			dataType: "xml",
			success: parser
		}); // close ajax
	}

	function parseABOUT(xml) {
		var html;
		html = '<div class="clearfix" id="about-wrapper">';
		$(xml).find('Me').each( function() {
			var name = $(this).find("Name").text();

			html += '<header><h2>'+name+'</h2></header>';

		});
		html += '</div>';
		$(html).appendTo("div#main");
	}

	function parseBLOG(xml) {
		var html;
		html = '<div class="clearfix" id="blog-wrapper">';
		$(xml).find('item').each( function() {
			var title = $(this).find('title').text();
			date = $(this).find('pubDate').text();
			content = $(this).find('description').text();
			postLink = $(this).find('link').text();

			html += '<article><header><h2><a href="' + postLink + '">' + title + '</a></h2></header>';
			html += '<section><p>' + content + '</p></section>';
			html += '</article>';
		});
		html+= '</div>';
		$(html).appendTo("div#main");
	}

	function parseGIT(xml) {
		var html;
		html = '<div class="clearfix" id="git-wrapper">';
		$(xml).find('Project').each( function() {
			var name = $(this).find("Name").text();
			var desc = $(this).find("Description").text();
			html += '<div class="gitProject"><h2>'+name+'</h2><p>'+desc+'</p>';
			$(this).find("Languages").each( function() {
				html += '<ul id="languages">';
				$(this).find("Language").each( function() {
					var lang = $(this).text();
					html += '<li class="'+lang+'">'+lang+'</li>';
				});
				html += '</ul>';
			});
			$(this).find("Libraries").each( function() {
				html += '<ul id="libraries">';
				$(this).find("Library").each( function() {
					var lib = $(this).text();
					html += '<li class="'+lib+'">'+lib+'</li>';
				});
				html += '</ul>';
			});
			$(this).find("Frameworks").each( function() {
				html += '<ul id="frameworks">';
				$(this).find("Framework").each( function() {
					var frame = $(this).text();
					html += '<li class="'+frame+'">'+frame+'</li>';
				});
				html += '</ul>';
			});
			var git = $(this).find("Github").text();
			html += '<a class="github" href="'+git+'">Github</a>'
			html += '</div>'
		});
		$(html).appendTo("div#main");
	}

	$('nav a').click( function(mouseEvent) {
		mouseEvent.preventDefault();
		var divIdent = $(this).attr('id') +'-wrapper';
		if ($(this).is('.active')) {

		} else {
			$('div#main').slideUp();
			$('div#main').children().css('display', 'none');
			$('div#'+divIdent).fadeIn(1000).parent().slideDown(500);
			$(this).addClass('active');
			$('nav a').not(this).removeClass('active');
		}

		return false;
	});
});